package com.cheer.listener.delay;

import cn.hutool.core.date.DateUtil;
import com.cheer.entity.MallProductBookStoreEntity;
import com.cheer.entity.MallProductEntity;
import com.cheer.service.MallCategoryProductRelationService;
import com.cheer.service.MallProductBookStoreService;
import com.cheer.service.MallProductService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author chufangzheng
 */
@Data
@Accessors(chain = true)
@ApiModel("ProductBookStockDelay")
@Slf4j
@ToString
public class ProductBookStockDelay implements Delayed, Runnable{

    @ApiModelProperty("预定库存ID")
    private Long bookStoreId;

    @ApiModelProperty("执行时间")
    private Long second;

    @ApiModelProperty("预定service")
    private MallProductBookStoreService mallProductBookStoreService;

    @ApiModelProperty("产品service")
    private MallProductService mallProductService;

    @ApiModelProperty("分类排序Service")
    private MallCategoryProductRelationService mallCategoryProductRelationService;


    @Override
    public void run() {

        log.info(" ------开始执行预定------ ");
        // 获取记录
        MallProductBookStoreEntity mallProductBookStoreEntity = mallProductBookStoreService.getById(bookStoreId);
        if(mallProductBookStoreEntity == null){
            return;
        }
        log.info("消费ID信息：" + mallProductBookStoreEntity.getId());
        // 获取执行时间
        Long recordSecond = mallProductBookStoreEntity.getBookDate().getTime()/1000;
        if(second.equals(recordSecond) == false){
            return;
        }
        MallProductEntity product = mallProductService.getById(mallProductBookStoreEntity.getProductId());
        product.setStock(product.getStock() + mallProductBookStoreEntity.getBookNum());
        product.setReset(false);
        product.setIsBookStock(0);
        mallProductService.updateById(product);
        mallProductBookStoreService.removeById(bookStoreId);
        // 更新产品分类中的秒数
        mallCategoryProductRelationService.updateCountDownSecondByProductId(product.getId(), null);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        Long currentSecond = DateUtil.currentSeconds();
        // 预定时间 - 当前时间
        return TimeUnit.SECONDS.toSeconds((second -  currentSecond));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ProductBookStockDelay that = (ProductBookStockDelay) o;
        return Objects.equals(bookStoreId, that.bookStoreId) &&
               Objects.equals(second, that.second);
    }

    @Override
    public int hashCode() {
        return Objects.hash(bookStoreId, second);
    }

    @Override
    public int compareTo(Delayed delayed) {
        ProductBookStockDelay productBookStockDelay = (ProductBookStockDelay)delayed;
        return this.second.compareTo(productBookStockDelay.getSecond());
    }

}
